package com.yanqu.road.dao.impl.beautyescort;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.beautyescort.EscortAttackRecord;
import com.yanqu.road.entity.beautyescort.EscortBeautyMapNpc;
import com.yanqu.road.entity.beautyescort.EscortUserData;
import com.yanqu.road.entity.beautyescort.UserDataParams;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class EscortUserDataDaoImpl extends TempDataObjectDao<EscortUserData> {


    public static String getEscortUserDataSql = "select * from `t_c_escort_user` ";
    public Map<Long, EscortUserData> getEscortUserDataMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, EscortUserData> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getEscortUserDataSql);
            rs = call.executeQuery();
            while (rs.next()) {
                EscortUserData data =  parse(rs);
                result.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String insertEscortUserDataSql = "insert into `t_c_escort_user`(userId,serverId,clientList,roadMapUser,roadMapNPCUser,totalGlamour,unionLv,lastReadReportTime,enemyList,resetTime,dayResetTime,dayEventNum,params,attackRecord) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(EscortUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getClientList(), ",")));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getRoadMapUser())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getRoadMapNPCUser())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTotalGlamour()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getUnionLv()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLastReadReportTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getEnemyList(), ",")));
        para.put(i++, new DbParameter(Types.BIGINT, data.getResetTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getDayResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDayEventNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getParams())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getAttackRecord())));
        if (getDb().executeNoneQuery(insertEscortUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateEscortUserDataSql = "update `t_c_escort_user` set serverId=?,clientList=?,roadMapUser=?,roadMapNPCUser=?,totalGlamour=?,unionLv=?,lastReadReportTime=?,enemyList=?,resetTime=?,dayResetTime=?,dayEventNum=?,params=?,attackRecord=? where userId=?";

    @Override
    protected boolean updateData(EscortUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getClientList(), ",")));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getRoadMapUser())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getRoadMapNPCUser())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTotalGlamour()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getUnionLv()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLastReadReportTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getEnemyList(), ",")));
        para.put(i++, new DbParameter(Types.BIGINT, data.getResetTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getDayResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDayEventNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getParams())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getAttackRecord())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateEscortUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected EscortUserData parseData(ResultSet rs) throws SQLException {
        EscortUserData escortUserData = new EscortUserData();
        escortUserData.setUserId(rs.getLong("userId"));
        escortUserData.setServerId(rs.getLong("serverId"));
        escortUserData.setClientList(StringUtils.stringToIntegerList(rs.getString("clientList"), ","));
        escortUserData.setRoadMapUser(JSON.parseObject(rs.getString("roadMapUser"), new TypeReference<Map<Integer, Long>>() {}));
        escortUserData.setRoadMapNPCUser(JSON.parseObject(rs.getString("roadMapNPCUser"), new TypeReference<Map<Integer, EscortBeautyMapNpc>>() {}));
        escortUserData.setTotalGlamour(rs.getLong("totalGlamour"));
        escortUserData.setUnionLv(rs.getInt("unionLv"));
        escortUserData.setLastReadReportTime(rs.getInt("lastReadReportTime"));
        escortUserData.setResetTime(rs.getLong("resetTime"));
        String columnStr = rs.getString("enemyList");
        if(!StringUtils.isNullOrEmpty(columnStr)) {
            escortUserData.setEnemyList(StringUtils.stringToLongList(columnStr, ","));
        }
        escortUserData.setDayResetTime(rs.getLong("dayResetTime"));
        escortUserData.setDayEventNum(rs.getInt("dayEventNum"));
        columnStr = rs.getString("params");
        if(!StringUtils.isNullOrEmpty(columnStr)){
            escortUserData.setParams(JSON.parseObject(columnStr,  new TypeReference<UserDataParams>(){}));
        }
        columnStr = rs.getString("attackRecord");
        if(!StringUtils.isNullOrEmpty(columnStr)){
            escortUserData.setAttackRecord(JSON.parseObject(columnStr,  new TypeReference<Map<Long, EscortAttackRecord>>(){}));
        }
        return escortUserData;
    }
}
